﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using CrushGame.DelegateSpace;

namespace CrushGame
{
    /// <summary>
    /// Controls a specific person, and handles input. This class declares the input events, and 
    /// </summary>
    public class Controller : IControllerInterface
    {
        //Necessary and assigned player
        Player _player;

        /// <summary>
        /// The player this controller is assigned to
        /// </summary>
        public Player Player
        {
            get { return _player; }
            set { _player = value; }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public Controller()
        {
            //We can't do anything if we are default constructed
        }

        /// <summary>
        /// This constructor lets us sign up for our input events and assign our player at the same time
        /// </summary>
        /// <param name="InputController">The game class with the event we need to sign up for</param>
        /// <param name="ThePlayer">The player we control</param>
        public Controller(TheCrushGame InputController, Player ThePlayer)
        {
            //Add our handler to the event
            InputController.InputDetected += new InputEventHandler(HandleInput);

            //Set our player to the player we were told about
            _player = ThePlayer;
        }

        /// <summary>
        /// Handles input sent from the Game's input functionality
        /// </summary>
        /// <param name="Sender">Sender of the Event</param>
        /// <param name="e">Arguments the event needs to process</param>
        void HandleInput(object Sender, InputEventArgs e)
        {
            //We will move in the corresponding direction when input happens
            switch (e.PressedKey)
            {
                case InputCommand.CMD_Move_Up:
                    AccelUp(e.Continued);
                    break;
                case InputCommand.CMD_Move_Left:
                    AccelLeft(e.Continued);
                    break;
                case InputCommand.CMD_Move_Down:
                    AccelDown(e.Continued);
                    break;
                case InputCommand.CMD_Move_Right:
                    AccelRight(e.Continued);
                    break;
                default:
                    break;
            }
        }

        //Acceleration helpers
        public void AccelLeft(bool RemainsDown)
        {
            //For the sake of rigging we will just set the
            //person's velocity in this direction. TODO: Add Acceleration
            Player.AccelerationRate += new Vector2(-25.0f, 0.0f);

            //Maybe do some animation stuff? right now we don't need it but we will
            //eventually
            if (RemainsDown)
            {
            
            }
        }

        public void AccelRight(bool RemainsDown)
        {
            //For the sake of rigging we will just set the
            //person's velocity in this direction. TODO: Add Acceleration
            Player.AccelerationRate += new Vector2(25.0f, 0.0f);

            //Maybe do some animation stuff? right now we don't need it but we will
            //eventually
            if (RemainsDown)
            {
                
            }
        }

        public void AccelDown(bool RemainsDown)
        {
            //For the sake of rigging we will just set the
            //person's velocity in this direction. TODO: Add Acceleration
            Player.AccelerationRate += new Vector2(0.0f, 25.0f);

            //Maybe do some animation stuff? right now we don't need it but we will
            //eventually
            if (RemainsDown)
            {
                
            }
        }

        public void AccelUp(bool RemainsDown)
        {
            //For the sake of rigging we will just set the
            //person's velocity in this direction. TODO: Add Acceleration
            Player.AccelerationRate += new Vector2(0.0f, -25.0f);

            //Maybe do some animation stuff? right now we don't need it but we will
            //eventually
            if (RemainsDown)
            {
                
            }
        }

        //More basic control functionality here
        /// <summary>
        /// Updates the controller, and anything that might need to happen incrementally
        /// </summary>
        public void Update()
        {
            //Nothing yet
        }

    }
}
